<?
/*
    topGames: an iRC games bot
    Copyright (C) 2008 Marcos García <marcosgdf@gmail.com>
    
	This file is part of topGames

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

class topgames {
	var $dbconnected;
	var $dbsock;

 	//mysql functions
	/**
	 * Connects to a mySQL server
	 * @return unknown_type
	 */
	function dbConnect() {
		if (DB_METHOD == 'socket') $host = ':'.DB_SOCK;
		elseif (DB_METHOD == 'ip') $host = DB_HOST.':'.DB_PORT;
		$this->dbsock = @mysql_connect($host,DB_USER,DB_PASS);
		if (!$this->dbsock) {
            global $_CFG;
			$this->dbconnected = false;
			die('[-] mySQL Error');
			//we have to put something here!
		}
		else {
			$this->dbconnected = true;
			return $this->dbsock;
		}
	}
	/**
	 * Returns wether the bot is connected to the database or not
	 * @return unknown_type
	 */
	function isDbConnected() {
		return $this->dbconnected;
	}
	/**
	 * Selects the database in the mysql Server
	 * @return unknown_type
	 */
	function dbSelect() {
		mysql_select_db(DB_NAME);
	}
	/**
	 * Sends a query to the mysql Server
	 * @param $query
	 * @return unknown_type
	 */
	function dbSend($query) {
		$q = mysql_query($query) or die($this->dbError());
		return $q;
	}
	/**
	 * It does this function when an error is detected in the database
	 * @return unknown_type
	 */
	function dbError() {
		//we have to put something here!
	}
 
	// nick functions
	/**
	 * It checks wether a nick is in the DB or not. It it is, it returns the ID of the nick.
	 * @param $nick
	 * @return unknown_type
	 */
	function isNickInDB($nick) {
		$query = $this->dbSend("SELECT `id` FROM ".DB_PREFIX."data WHERE `nick`='$nick'");
		if (mysql_num_rows($query)) return mysql_result($query,0);
  		else return false;
	}
}

?>